package com.dhh.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import com.dhh.vo.Weight;

public class WeightDAO {
private DataBaseHelper db;
	
	public WeightDAO(DataBaseHelper db){
		this.db = db;
	}
	
	public Weight getWeightById(String id){
		Weight w = new Weight();
		try{
			String whereSQL = "_id='"+id+"'";
			Cursor cursor = db.getReadableDatabase().query(true, "weight", Weight.getColumns(), whereSQL,null, null, null, null, null);
			if(cursor!=null && cursor.moveToFirst()){
				w.setId(cursor.getString(0));
				w.setDate(cursor.getString(1));
				w.setWeight(cursor.getString(2));
			}
			cursor.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return w;
	}
	
	public List<Weight> getlastWeight(int limit){
		List<Weight> ws = new ArrayList<Weight>();
		try{
			String l = limit>0 ? String.valueOf(limit):null;
			Cursor cursor = db.getReadableDatabase().query(true, "weight", Weight.getColumns(), null,null, null, null, "date desc", l);
			if(cursor!=null && cursor.getCount()>0){
				cursor.moveToFirst();
				do{
					Weight w = new Weight();
					w.setId(cursor.getString(0));
					w.setDate(cursor.getString(1));
					w.setWeight(cursor.getString(2));
					ws.add(w);
				}while(cursor.moveToNext());
			}
			cursor.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return ws;
	}
	
	public List<Weight> getWeightLogs(int limit){
		List<Weight> ws = new ArrayList<Weight>();
		try{
			String sl = limit>0 ? String.valueOf(limit):null;
			Cursor cursor = db.getReadableDatabase().query(true, "weight", Weight.getColumns(), null,null, null, null, "date desc", sl);
			if(cursor!=null && cursor.getCount()>0){
				cursor.moveToFirst();
				do{
					Weight w = new Weight();
					w.setId(cursor.getString(0));
					w.setDate(cursor.getString(1));
					w.setWeight(cursor.getString(2));
					ws.add(w);
				}while(cursor.moveToNext());
			}
			cursor.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return ws;
	}
	
	public String lastDate(Calendar c){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String whereSQL = "date in (";
		String[] dates = new String[7];
		for(int i=0; i<7; i++){
			dates[i] = df.format(c.getTime());
			whereSQL += "'"+dates[i]+"'";
			c.add(Calendar.DATE, -1);
			
			if(i<6)
				whereSQL += ",";
		}
		whereSQL += ")";
		return whereSQL;
	}
	
	public Weight getInitWeight(){
		Weight w = new Weight();
		try{
			String whereSQL = "date=(select min(date) from weight)";
			Cursor cursor = db.getReadableDatabase().query(true, "weight", Weight.getColumns(), whereSQL,null, null, null, null, null);
			if(cursor!=null && cursor.moveToFirst()){
				w.setId(cursor.getString(0));
				w.setDate(cursor.getString(1));
				w.setWeight(cursor.getString(2));
			}
			cursor.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return w;
	}
	
	public Weight getCurrentWeight(){
		Weight w = new Weight();
		try{
			String whereSQL = "date=(select max(date) from weight)";
			Cursor cursor = db.getReadableDatabase().query(true, "weight", Weight.getColumns(), whereSQL,null, null, null, null, null);
			if(cursor!=null && cursor.moveToFirst()){
				w.setId(cursor.getString(0));
				w.setDate(cursor.getString(1));
				w.setWeight(cursor.getString(2));
			}
			cursor.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return w;
	}
	
	public boolean addWeight(Weight w){
		boolean isSuc = false;
		ContentValues initialValues = createContentValues(w);
		try{
			db.getWritableDatabase().insert("weight", null, initialValues);
			isSuc = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(db!=null) db.close();
		}
		return isSuc;
	}
	
	private ContentValues createContentValues(Weight w){
		ContentValues values = new ContentValues();
		values.put(Weight.C_DATE, w.getDate());
		values.put(Weight.C_WEIGHT, w.getWeight());
		return values;
	}
	
	public boolean deleteWeightLog(String id){
		boolean isSuc = false;
		
		try{
			db.getWritableDatabase().delete("weight", "_id=?",new String[] {id});
			isSuc = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return isSuc;
	}
	
	public Weight getWeightLog(String date){
		Weight w = new Weight();
		try{
			String whereSQL = "date='"+date+"'";
			Cursor cursor = db.getReadableDatabase().query(true, "weight", Weight.getColumns(), whereSQL,null, null, null, null, null);
			if(cursor!=null && cursor.moveToFirst()){
				w.setId(cursor.getString(0));
				w.setDate(cursor.getString(1));
				w.setWeight(cursor.getString(2));
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return w;
	}
}
